#include "importgoods.h"
#include "ui_importgoods.h"

ImportGoods::ImportGoods(QWidget *parent,int sid) :
    QDialog(parent),
    ui(new Ui::ImportGoods)
{
    ui->setupUi(this);
    m_sid = sid;
    this->myDatabase.getConnection();
}

ImportGoods::~ImportGoods()
{
    delete ui;
}

void ImportGoods::on_Quit_clicked()
{
    this->~ImportGoods();
}


void ImportGoods::on_Ensure_clicked()
{
    QString cat = ui->cat->currentText();
    QString gname = ui->gname->text();
    int gnum = ui->gnum->text().toInt();
    float gprice = ui->gprice->text().toFloat();
    QString ginfo = ui->ginfo->text();

    QSqlQuery sql;

    sql.prepare("SELECT* FROM tb_goodsinstore WHERE sid = :sid AND gname = :gname");
    sql.bindValue("sid",m_sid);
    sql.bindValue(":gname",gname);
    if(sql.first()){
        QMessageBox::information(this,"提示","已存在该货物！",QMessageBox::Yes);
        this->~ImportGoods();
    }


    QSqlQuery sql_cat;
    QSqlQuery sql_goods;
    QSqlQuery sql_goodsinStore;

    sql_cat.prepare("SELECT* FROM tb_goods_cat WHERE cname = :cat");
    sql_cat.bindValue(":cat",cat);
    sql_cat.exec();
    if(!sql_cat.first()){
        sql_cat.prepare("INSERT INTO tb_goods_cat(cname,cnum) values (:cat,:cnum)");
        sql_cat.bindValue(":cat",cat);
        sql_cat.bindValue(":cnum",gnum);
        sql_cat.exec();
        qDebug()<<"新商品种类添加成功！";
    }
    else{
        int num = sql_cat.value("cnum").toInt();
        qDebug()<<num;
        qDebug()<<gnum;
        qDebug()<<cat;
        sql_cat.prepare("UPDATE tb_goods_cat SET cnum = :totalnum WHERE cname = :cname");
        sql_cat.bindValue(":totalnum",num+gnum);
        sql_cat.bindValue(":cname",cat);
        sql_cat.exec();
        qDebug()<<"商品种类更新成功！";
    }


    sql_goods.prepare("SELECT* FROM tb_goods WHERE gname = ':gname'");
    sql_goods.bindValue(":gname",gname);
    sql_goods.exec();
    if(!sql_goods.first()){
        sql_goods.prepare("INSERT INTO tb_goods(gname,gnum,gintro,gcat) values (:gname,:gnum,:gintro,:gcat)");
        sql_goods.bindValue(":gname",gname);
        sql_goods.bindValue(":gnum",gnum);
        sql_goods.bindValue(":gintro",ginfo);
        sql_goods.bindValue(":gcat",cat);
        sql_goods.exec();
        qDebug()<<"货物添加成功！";
    }
    else{
        sql_cat.prepare("SELECT* FROM tb_goods_cat WHERE cname = :cat");
        sql_cat.bindValue(":cat",cat);
        sql_cat.exec();
        sql_cat.first();
        int num = sql_cat.value("cnum").toInt();
        sql_cat.prepare("UPDATE tb_goods_cat SET cnum = :totalnum WHERE gname = :gname");
        sql_cat.bindValue(":totalnum",num+gnum);
        sql_cat.bindValue(":gname",gname);
        sql_cat.exec();
        qDebug()<<"新货物添加成功！";
    }

    QSqlQuery sql_;

    qDebug()<<gname;
    sql_.prepare("SELECT* FROM tb_goods WHERE gname = :gname");
    sql_.bindValue(":gname",gname);
    sql_.exec();
    int gid = 1;
    if(sql_.first())
        gid = sql_.value("gid").toInt();
    qDebug()<<gid;
    sql_goodsinStore.prepare("SELECT* FROM tb_goodsinstore WHERE sid = :sid AND gid = :gid");
    sql_goodsinStore.bindValue("sid",m_sid);
    sql_goodsinStore.bindValue(":gid",gid);
    if(!sql_goodsinStore.first()){
        qDebug()<<"准备添加";
        QSqlQuery sql_import;
        sql_import.prepare("INSERT INTO tb_goodsinstore(sid,gid,name,num,price,info) values (:sid,:gid,:name,:num,:price,:info)");
        sql_import.bindValue(":sid",m_sid);
        sql_import.bindValue(":gid",gid);
        sql_import.bindValue(":name",gname);
        sql_import.bindValue(":num",gnum);
        sql_import.bindValue(":price",gprice);
        sql_import.bindValue(":info",ginfo);
        sql_import.exec();
        QMessageBox::information(this,"提示","新商店货物添加成功！",QMessageBox::Yes);
    }

    this->~ImportGoods();
}

